/**
 * @Author: 陈瑞鹏
 * @Date:   2022-10-17 10:04:44
 * @Last Modified by:   陈瑞鹏
 * @Last Modified time: 2022-10-17 11:39:47
 */

import jsonwebtoken from 'jsonwebtoken';
const secret = 'guardbattle';

export default class JWT {
  /**
   * 生成
   * @param value 
   * @param expires 
   * @returns 
   */
  static generate(value: any, expires?: '7 day'): string {
    try {
      let token = 'Bearer ' + jsonwebtoken.sign(value, secret);
      return token;
    } catch (error) {
      return 'error';
    }
  }

  /**
   * 校验
   * @param token 
   */
  static verify(token: string): false | jsonwebtoken.JwtPayload {
    try {
      return jsonwebtoken.verify(token.replace('Bearer ', ''), secret) as jsonwebtoken.JwtPayload
    } catch (error) {
      return false;
    }
  }
}